﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lenovo.CFI.BLL;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;

namespace Lenovo.CFI.Web.VP
{
    public partial class TopicReviewer : System.Web.UI.UserControl
    {
        TopicBl topicBl = new TopicBl();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int? id = UrlHelper.GetQueryStringID();
                Topic topic = null;
                if (id.HasValue)
                {
                    topic = topicBl.GetTopicByID(id.Value);
                }
                if (topic == null) ErrorHelper.RaiseNoValueError();

                ViewState["topicid"] = topic.ID;

                List<string> reviewers = new List<string>();
                foreach (Common.TopicReviewer tr in topicBl.GetReviewer(topic.ID))
                    reviewers.Add(tr.Reviewer);
                this.UacReviewers.Value = CodeList.ConvertListToString(reviewers);
                ScriptManager.RegisterStartupScript(this, this.GetType(), this.UacReviewers.ClientID,
                    this.UacReviewers.GetJsInitFunction(false) + ";", true);
            }

        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            int topicID = Convert.ToInt32(ViewState["topicid"]);

            try
            {
                List<string> reviewers = new List<string>();
                reviewers.AddRange(CodeList.SplitStringToList(this.UacReviewers.Value));
                topicBl.EditTopicReivewers(topicID, reviewers);

                MessageHelper.Show("Success!", this.Page);

                ScriptManager.RegisterStartupScript(this.BtnSave, this.BtnSave.GetType(), this.UacReviewers.ClientID,
                    this.UacReviewers.GetJsInitFunction(false) + ";", true);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.Show(ex.Message, this.Page);
            }
        }

        protected void BtnFinish_Click(object sender, EventArgs e)
        {
            int topicID = Convert.ToInt32(ViewState["topicid"]);

            try
            {
                topicBl.ReadyForReview(topicID);

                MessageHelper.Show("Success!", this.Page);

                ScriptManager.RegisterStartupScript(this.BtnFinish, this.BtnFinish.GetType(), this.UacReviewers.ClientID,
                    this.UacReviewers.GetJsInitFunction(false) + ";", true);

                System.Web.UI.ScriptManager.RegisterStartupScript(this.BtnFinish, this.Page.GetType(), "Close",
                    String.Format(@"setTimeout(""window.location='{0}';"", 1000);", UrlHelper.GenerateUrl("topiclist")), true);

            }
            catch (ApplicationException ex)
            {
                MessageHelper.Show(ex.Message, this.Page);
            }
        }
    }
}